// pages/video/video.js
import request from '../../utils/request';

Page({

  /**
   * 页面的初始数据
   */
  data: {
    videoGroupList:[],
    navId:"",
    videoList:[],
    videoContext:"",
    videoId:"",
    videoUpdateTime: [],
    refresherTriggered: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
        this.getVideoGroupList();
        // this.getVideoGroupList(this.data.navId);
  },

  async getVideoGroupList(){
    let videoGroupListData = await request('/video/group/list');
    this.setData({
      videoGroupList: videoGroupListData.data.slice(0,10),
      navId : videoGroupListData.data[0].id
    })
    this.getVideoList(this.data.navId);
  },

  async getVideoList(navId){
      let videoListData = await request('/video/group',{id:navId})
      wx.hideLoading();
      this.setData({
        videoList: videoListData.datas.slice(0,3),
        refresherTriggered:false
      })
  },

  changeNav(event){
      let navId = event.currentTarget.id;
      console.log("navId",navId)
      this.setData({
        navId:navId>>>0,
        videoList:[]
      })
      //
      wx.showLoading({
        title: '正在加载',
      })
      this.getVideoList(navId);
  },

  handlePlay(event){
    if(event.currentTarget.dataset.type == 'video'){
        return;
    }
    let vid = event.currentTarget.id;
    event.currentTarget.dataset.type == 'image' && this.vid !== vid && this.videoContext && this.videoContext.stop();
    // console.log("vid",vid);
    // this.vid = vid;
    let videoContextCurrent = wx.createVideoContext(vid);
    let {videoUpdateTime} = this.data;
    let videoItem = videoUpdateTime.find(item => item.vid==vid)
    if(videoItem){
      videoContextCurrent.seek(videoItem.currentTime)
    }
    videoContextCurrent.play();
    this.setData({
      videoContext:videoContextCurrent,
      videoId: vid
    })
  },

  handleTimeUpdate(event){
        let videoTimeObj = {
          vid: event.currentTarget.id,
          current: event.detail.currentTime
        }
        let {videoUpdateTime} = this.data;
        let videoItem = videoUpdateTime.find(item => item.vid === videoTimeObj.vid);
        if(videoItem){
          videoItem.currentTime = event.detail.currentTime;
        }else{
          videoUpdateTime.push(videoTimeObj);
        }
        this.setData({
          videoUpdateTime
        })
  },

  handleEnded(event){
    let {videoUpdateTime} = this.data;
    videoUpdateTime.splice(videoUpdateTime.findIndex(item => event.currentTarget.id == item.vid),1);
    this.setData({
      videoUpdateTime
    })
  },

  handleRefresher(){
    console.log(32323)
    this.setData({
      refresherTriggered:true
    })
    this.getVideoList(this.data.navId);
  },

  handleToLower(){

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage({from}) {
      console.log(from);
      return {
        title: '转发哈哈哈',
        page: '/pages/personal/personal', //?
        imageUrl: '/static/images/nvsheng.jpg'
      }
  }
})